/**
 * Created by IntelliJ IDEA.
 * User: mabo
 * Date: 2010-3-17
 * Time: 9:45:21
 * To change this template use File | Settings | File Templates.
 */


var sendMail = function (confingId) {
    var hrConfig;
    SendMail.getHrConfig(confingId, function(r) {
        hrConfig = r;
    });

    var spot = new Ext.ux.Spotlight({
        easing: 'easeOut',
        duration: .3
    });


    var myWin = Ext.extend(Ext.Window, {
        y:200,
        width:300,
        height:250,
        draggable:false,//不可拖动
        toggle: function(on) {
            this.setDisabled(!on);
            this.show();
        },
        tools:[
            {
                id:'close',
                qtip:'结束发送流程',
                handler: function(event, toolEl, panel) {
                    updateSpot(false);
                    win1.close();
                    win2.close();
                    win3.close();
                }
            }
        ]
    });

    var flow = {};

    var win1, win2, win3;
    var updateSpot = function(id) {
        if (typeof id == 'string') {
            spot.show(id);
        } else if (!id && spot.active) {
            spot.hide();
        }
        win1.toggle(id == win1.id);
        win2.toggle(id == win2.id);
        win3.toggle(id == win3.id);
    };

    var sendMailRgValue = 0;
    win1 = new myWin({
        id:'win1',
        title:'简历发送流程',
        x:(document.documentElement.clientWidth / 3) - 320,
        bodyStyle: 'padding:10px 15px;',
        buttons:[
            new Ext.Button({
                text:'下一步',
                handler:function () {
                    var items = Ext.getCmp('sendMailRg_id').items
                            for(var i = 0 ; i < items.length; i ++)
                    {
                        if (items.get(i).checked) {
                            sendMailRgValue = items.get(i).inputValue;
                        }
                    }
                    if (sendMailRgValue != 0) {
                        updateSpot('win2');
                        win2Login();
                    } else {
                        Ext.MessageBox.alert('简历发送流程', '请选择发送方式');
                    }
                }
            })
        ]
    }).show();


    win2 = new myWin({
        id:'win2',
        title:'简历发送流程',
        bodyStyle: 'padding:10px 15px;',
        x:(document.documentElement.clientWidth / 3),
        buttons:[
            new Ext.Button({
                text:'上一步',
                handler: updateSpot.createDelegate(this, ['win1'])
            }),
            new Ext.Button({
                text:'下一步',
                id:'win2_butNext_id',
                handler: function() {
                    win3Login();
                    updateSpot('win3');
                }

            })
        ]
    }).show();

    win3 = new myWin({
        id:'win3',
        title:'简历发送流程',
        bodyStyle: 'padding:10px 15px;',
        x:(document.documentElement.clientWidth / 3) + 320,
        buttons:[
            new Ext.Button({
                text:'上一步',
                handler: updateSpot.createDelegate(this, ['win2'])
            }),
            new Ext.Button({
                text:'发送简历',
                id:'win3_sendMail_but_id'
            })
        ]
    }).show();

    var win2Login = function() {


        //加载动画，'edit_form'为遮盖对象

        var mailFormSet = new Ext.form.FieldSet({
            id:'mailFormSet',
            title: '填写邮箱信息',
            autoHeight:true,
            items:[
                new Ext.form.TextField({
                    fieldLabel: '登录名',
                    id: 'mailName_inp_id',
                    name:'sendMail',
                    value:''
                }),
                new Ext.form.TextField({
                    fieldLabel: '密码',
                    id: 'mailPwd_inp_id',
                    inputType:'password',
                    name:'mailPwd',
                    value:''
                }),
                new Ext.form.TextField({
                    fieldLabel: 'SMTP',
                    id: 'mailSmtp_inp_id',
                    name:'smtp',
                    value:''
                })
            ],
            buttons:[
                new Ext.Button({
                    text:'保存',
                    handler:function() {
                        var mask = new Ext.LoadMask('mailFormSet', {
                            msg:'数据提交中'
                        });
                        mask.show();
                        var hc = {};
                        hc['id'] = confingId;
                        for (var i = 0; i < mailFormSet.items.length; i ++) {
                            hc[mailFormSet.items.get(i).name] = mailFormSet.items.get(i).getValue();
                        }
                        SendMail.saveSendMailInfo(hc, function(r) {
                            mask.hide();
                            Ext.MessageBox.alert('保存邮箱信息', '保存成功');
                        });
                    }
                })
            ]
        })


        // rgVal = 2
        var weblFormSet = new Ext.form.FieldSet({
            id:'weblFormSet',
            title: '填写' + hrConfig.hrWebName + '登录信息',
            autoHeight:true,
            items:[
                new Ext.form.TextField({
                    fieldLabel: '登录用户',
                    id: 'webName_inp_id',
                    name:'webName',
                    value:''
                }),new Ext.form.TextField({
                    fieldLabel: '登录密码',
                    inputType:'password',
                    id: 'webPassWord_inp_id',
                    name:'webPassWord',
                    value:''
                })
            ],
            buttons:[
                new Ext.Button({
                    text:'保存',
                    handler:function() {
                        var mask = new Ext.LoadMask('weblFormSet', {
                            msg:'数据提交中'
                        });
                        mask.show();
                        var hc = {};
                        hc['id'] = confingId;
                        for (var i = 0; i < weblFormSet.items.length; i ++) {
                            hc[weblFormSet.items.get(i).name] = weblFormSet.items.get(i).getValue();
                        }
                        SendMail.saveWebInfo(hc, function(r) {
                            mask.hide();
                            if (r) {
                                Ext.MessageBox.alert('保存求职网站登录信息', '保存成功');
                            } else {
                                Ext.MessageBox.alert('保存求职网站登录信息', '保存失败');
                            }
                        });
                    }
                }),
                new Ext.Button({
                    text:'登录网站',
                    handler:function() {
                        var mask = new Ext.LoadMask('weblFormSet', {
                            msg:'登录中'
                        });
                        mask.show();
                        var hc = {};
                        hc['id'] = confingId;
                        for (var i = 0; i < weblFormSet.items.length; i ++) {
                            hc[weblFormSet.items.get(i).name] = weblFormSet.items.get(i).getValue();
                        }
                        SendMail.saveWebInfo(hc, function(r) {
                        });
                        SendMail.loginWeb(confingId, function(r) {
                            Ext.getCmp('win2_butNext_id').setDisabled(false);
                            mask.hide();
                            if (r) {
                                Ext.MessageBox.alert('登录求职网站', '成功');
                            } else {
                                Ext.MessageBox.alert('登录求职网站', '失败');
                            }
                        });
                    }
                })
            ]
        });


        win2.removeAll();

        switch (sendMailRgValue) {
            case '1':
                Ext.getCmp('win2_butNext_id').setDisabled(false)
                win2.items.add(mailFormSet);
                var mask = new Ext.LoadMask('win2', {
                    msg:'数据加载中'
                });
                mask.show();
                SendMail.getSendMailInfo(confingId, function(r) {
                    Ext.getCmp('mailName_inp_id').setValue(r[0] == null ? '' : r[0]);
                    Ext.getCmp('mailPwd_inp_id').setValue(r[1] == null ? '' : r[1]);
                    Ext.getCmp('mailSmtp_inp_id').setValue(r[2] == null ? '' : r[2]);
                    mask.hide();
                })
                break;
            case '2':
                Ext.getCmp('win2_butNext_id').setDisabled(true)
                win2.items.add(weblFormSet);
                var mask = new Ext.LoadMask('win2', {
                    msg:'数据加载中'
                });
                mask.show();
                SendMail.getHrConfig(confingId, function(r) {
                    Ext.getCmp('webName_inp_id').setValue(r.webName == null ? '' : r.webName);
                    Ext.getCmp('webPassWord_inp_id').setValue(r.webPassWord == null ? '' : r.webPassWord);
                    mask.hide();
                })
                break;
            case '3':
                break;
        }
        //win2.items.add(name);
        win2.doLayout();
    }


    var win3Login = function() {
        win3.removeAll();
        var win3FormSet = new Ext.form.FieldSet({
            id:'win3FormSet_id',
            title: '简历管理',
            autoHeight:true,
            layout:'column',
            defaults:{margin:'50 50 50 50'},
            items:[
                new Ext.Button({
                    text:'编辑HTML简历模板',
                    handler:function() {
                        openEditWin(confingId);
                    }
                }),
                new Ext.Button({
                    text:'预览HTML简历模板',
                    handler:function() {
                        new Ext.Window({
                            title:'简历预览',
                            width:700,
                            height:480,
                            bodyStyle: 'background-color:#ffffff;',
                            autoScroll:true,
                            autoLoad:{
                                url:path + '/hr/hrconfig.x?op=findHcClByExt&hc.id=' + confingId
                            },
                            loadMask:{msg:'数据加载中。。。'}
                        }).show();
                    }
                })
            ]
        });


        //sendMailRgValue ==2
        var webJianLiStore = new Ext.data.DirectStore({
            root:'results',
            fields:[
                'name',
                'id'
            ],
            directFn:SendMail.getJianLi,
            paramsAsHash:true,
            baseParams: {hrWeb:hrConfig.hrWebName}
        });


        var webQiuZhiStore = new Ext.data.DirectStore({
            root:'results',
            fields:[
                'name',
                'id'
            ],
            directFn:SendMail.getQiuZhiXin,
            paramsAsHash:true,
            baseParams: {hrWeb:hrConfig.hrWebName}
        });

        var win3WebLoginSet = new Ext.form.FieldSet({
            id:'win3FormSet_id',
            title: '简历管理',
            autoHeight:true,
            labelWidth:80,
            items:[
                new Ext.form.ComboBox({
                    id:'win3JLcBox_id',
                    valueField: "id",//取值列
                    displayField: "name",//默认数据
                    width:150,
                    store:webJianLiStore,
                    editable: false,
                    allowBlank: true,
                    forceSelection: true,
                    triggerAction: 'all',
                    fieldLabel: '选择简历'
                }),
                new Ext.form.ComboBox({
                    id:'win3QZXcBox_id',
                    valueField: "id",//取值列
                    displayField: "name",//默认数据
                    width:150,
                    store:webQiuZhiStore,
                    editable: false,
                    allowBlank: true,
                    forceSelection: true,
                    triggerAction: 'all',
                    fieldLabel: '选择求职信'
                })
            ]
        });


        switch (sendMailRgValue) {
            case '1':
                win3.items.add(win3FormSet);
                win3.doLayout();
                Ext.getCmp('win3_sendMail_but_id').on('click', function() {
                    var mask = new Ext.LoadMask('win3', {
                        msg:'简历发送中'
                    });
                    mask.show();
                    SendMail.sendMail(confingId, function(r) {
                        Ext.MessageBox.alert('发送简历', '发送成功');
                        mask.hide();
                        updateSpot(false);
                        win1.close();
                        win2.close();
                        win3.close();
                    })
                });
                break;
            case '2':
                win3.items.add(win3WebLoginSet);
                win3.doLayout();
                Ext.getCmp('win3_sendMail_but_id').on('click', function() {
                    alert(Ext.getCmp('win3JLcBox_id').getValue());
                    alert(Ext.getCmp('win3QZXcBox_id').getValue());
                });
                break;
            case '3':
                break;
        }

    }


    updateSpot(false);


    //准备win1需要的数据
    updateSpot('win1');
    var mask = new Ext.LoadMask('win1', {
        msg:'数据加载中'
    });
    mask.show();
    SendMail.getSendMailCount(confingId, function (r) {
        var rSet = new Ext.form.FieldSet({
            id:'sendMailRg_id',
            title: '选择简历发送方式',
            autoHeight:true,
            labelWidth:1,
            items:[
                new Ext.form.Radio({
                    name : "sendType",
                    inputValue : "1",
                    boxLabel : String.format("通过Email发送简历，有{0}个职位", r[0])
                }),
                new Ext.form.Radio({
                    name : "sendType",
                    inputValue : "2",
                    boxLabel : String.format("通过求职网站发送简历，有{0}个职位", r[1])

                }),
                new Ext.form.Radio({
                    name : "sendType",
                    inputValue : "3",
                    boxLabel : "全部通过求职网站发送简历"
                })
            ]
        });
        win1.items.add(rSet);
        win1.doLayout();
        mask.hide();

    });
    //setTimeout(updateSpot.createDelegate(this, ['win1']), 500);


}


